package com.flow.demo.module.gd.dal.redis;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.flow.demo.framework.common.util.json.JsonUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;

import static com.flow.demo.module.gd.dal.redis.RedisKeyConstants.SEND_CODE_PHONE;

@Repository
public class SendCodeRedisDAO {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    public String get(String phone) {
        String redisKey = formatKey(phone);
        return stringRedisTemplate.opsForValue().get(redisKey);
    }

    public void set(String phone,String code) {
        String redisKey = formatKey(phone);
        //设置有效时间5分钟
        stringRedisTemplate.opsForValue().set(redisKey, code, 300, TimeUnit.SECONDS);
    }

    public void delete(String accessToken) {
        String redisKey = formatKey(accessToken);
        stringRedisTemplate.delete(redisKey);
    }



    private static String formatKey(String phone){
        return String.format(SEND_CODE_PHONE,phone);
    }
}
